/**
 * Created by bai on 2016/11/27.
 */


jQuery(document).ready(function () {
    jQuery('#addForm').validate({
        rules: {
            goodsName: {required: true},
            // catId: { required: false },
            // catName: { required: false },
            url: {required: true},
            goodsCode: {required: true},
            goodsPrice: {required: true, number: true},
            originPrice: {required: true, number: true},
            rebate: {required: true, number: true},
            goodFrom: {required: true},
            keywords: {required: true},
            // starNum: { required: false },
            picUrls: {required: true},
            // createTime: { required: false },
            // updateTime: { required: false },
        },
        messages: {
            goodsName: {required: "商品名称不能为空！"},
            url: {required: "商品链接不能为空！"},
            goodsCode: {required: "商品编码不能为空！"},
            goodsPrice: {required: "商品价格不能为空！", number: "商品价格必须为数字！"},
            originPrice: {required: "商品原价不能为空！", number: "商品原价必须为数字！"},
            rebate: {required: "可返金额不能为空！", number: "可返金额必须为数字！"},
            goodFrom: {required: "商品来源不能为空！"},
            keywords: {required: "关键字不能为空"},
            picUrls: {required: "商品图片链接不能为空！"}
        },

        errorElement: 'font',
        errorClass: 'c1',
        focusInvalid: false,
        invalidHandler: function (event, validator) { //display error alert on form submit
            //$('.alert-danger', $('.login-form')).show();
            alert('error');
        },
        highlight: function (e) {
            jQuery(e).closest('.form-group').removeClass('has-info').addClass('has-error');
        },
        success: function (e) {
            jQuery(e).closest('.form-group').removeClass('has-error').addClass('has-info');
            jQuery(e).remove();
        },
        submitHandler: function (form) {
            jQuery('#addForm').ajaxSubmit({
                url: '/sys/goods/save.ajax',
                type: 'post',
                dataType: 'json',
                success: function (json, statusText, xhr, $form) {
                    if (json._success) {
                        window.location.href = '/sys/goods/index.html';
                    } else {
                        alert(json._msg);
                    }
                }
            });
        }
    });
});